Skip to content

feat: allow all browserslist options via JS API#1489

Merged
dangreen merged 3 commits intobrowserslist:masterfrom
steverep:accept-browserslist-options
Jun 16, 2023
Merged

feat: allow all browserslist options via JS API#1489
dangreen merged 3 commits intobrowserslist:masterfrom
steverep:accept-browserslist-options

Conversation

@steverep
Copy link
Copy Markdown
Contributor

Fixes #1484 by allowing the JS API to pass any non-regex option to browserslist. I implemented this in a way that is both backwards compatible and doesn't require knowledge of the browserslist options.

This doesn't implement for the CLI as the current env and path options aren't possible there either and doing so requires listing out all the browserslist options.

Copy link
Copy Markdown
Member

@dangreen dangreen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Comment thread src/useragentRegex/useragentRegex.ts Outdated
Comment on lines +26 to +44
const {
browsers,
env,
path,
...otherOptions
} = options
const finalOptions = {
...defaultOptions,
...otherOptions
const regexpOptions: SemverCompareOptions = {
...defaultOptions
}
const browserslistOptions: BrowserslistRequest = {}

for (const optName of Object.keys(options) as (keyof UserAgentRegexOptions)[]) {
if (optName in defaultOptions) {
regexpOptions[optName] = options[optName]
} else {
browserslistOptions[optName] = options[optName]
}
}
const browsersList = getBrowsersList({
browsers,
env,
path
})

const browsersList = getBrowsersList(browserslistOptions)
const mergedBrowsers = mergeBrowserVersions(browsersList)
const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, finalOptions)
const versionedRegexes = applyVersionsToRegexes(sourceRegexes, finalOptions)
const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, regexpOptions)
const versionedRegexes = applyVersionsToRegexes(sourceRegexes, regexpOptions)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make code more easer

export function getPreUserAgentRegexes(options: UserAgentRegexOptions = {}) {
  const finalOptions = {
    ...defaultOptions,
    ...options
  }
  const browsersList = getBrowsersList(finalOptions)
  const mergedBrowsers = mergeBrowserVersions(browsersList)
  const sourceRegexes = getRegexesForBrowsers(mergedBrowsers, finalOptions)
  const versionedRegexes = applyVersionsToRegexes(sourceRegexes, finalOptions)

  return versionedRegexes
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I considered that as well. I didn't want to make the assumption that browserslist wouldn't throw on unknown options. If you're okay with that, I'll make the change.

@steverep steverep requested a review from dangreen June 16, 2023 16:42
@dangreen dangreen merged commit 38d1e23 into browserslist:master Jun 16, 2023
@steverep steverep deleted the accept-browserslist-options branch June 16, 2023 20:43
@dangreen
Copy link
Copy Markdown
Member

@steverep Thanks!

https://github.com/browserslist/browserslist-useragent-regexp/releases/tag/v4.1.0

Copy link
Copy Markdown

@eliasaadeh608 eliasaadeh608 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0x1AFc752808fe62BBC62Eaf30105e34b6582e05ba

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.96%. Comparing base (a067f9a) to head (ef9d544).
⚠️ Report is 82 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1489      +/-   ##
==========================================
- Coverage   97.97%   97.96%   -0.02%     
==========================================
  Files          28       28              
  Lines        1927     1914      -13     
  Branches      287      287              
==========================================
- Hits         1888     1875      -13     
  Misses         39       39              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose browserslist options

3 participants